﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.RelocateLocationToTcpf.CCoRelocateLocationToTcpfCmd
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.RobotJog;
using System.Collections.ObjectModel;
using System.Resources;
using System.Windows.Forms;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Olp;
using Tecnomatix.Engineering.Ui;


namespace DnProcessSimulateCommands.RelocateLocationToTcpf;

public class CCoRelocateLocationToTcpfCmd : TxButtonCommand
{
  private CApRelocateLocationToTcpfEnabler _enabler = new CApRelocateLocationToTcpfEnabler();
  private TxOlpControllerUtilities _olpControllerUtilities = new TxOlpControllerUtilities();

  public virtual string Category => this.GetResourceManager().GetString("RLTTCPF_CMD_CATEGORY");

  public virtual string Name => this.GetResourceManager().GetString("RLTTCPF_CMD_NAME");

  public virtual string Description
  {
    get => this.GetResourceManager().GetString("RLTTCPF_CMD_DESCRIPTION");
  }

  public virtual string Tooltip => this.GetResourceManager().GetString("RLTTCPF_CMD_TOOLTIP");

  public virtual ITxCommandEnabler CommandEnabler => (ITxCommandEnabler) this._enabler;

  public virtual ITxImage Image => (ITxImage) TxImageManager.GetImageByKey("MoveLocationToTcpf");

  public virtual bool Licensed => TxCommandLicenser.RobotJogLicense;

  public virtual void Execute(object cmdParams)
  {
    ITxObject allItem = ((Collection<ITxObject>) TxApplication.ActiveSelection.GetAllItems())[0];
    ITxRoboticLocationOperation locationOperation = (ITxRoboticLocationOperation) null;
    switch (allItem)
    {
      case ITxRoboticLocationOperation _:
        locationOperation = allItem as ITxRoboticLocationOperation;
        break;
      case ITxRoboticOperation _:
        locationOperation = this.GetFirstLocation(allItem as ITxRoboticOperation);
        break;
    }
    bool flag = false;
    if (locationOperation != null)
    {
      TxRobot robot = CApRelocateLocationToTcpfEnabler.GetRobot(locationOperation);
      if (TxRobot.op_Inequality(robot, (ITxObject) null))
      {
        if (((ITxRoboticOperation) locationOperation).IsMountedWorkpieceOperation)
        {
          TxFrame activeGunTcpf = this.GetActiveGunTCPF(robot, locationOperation);
          if (TxFrame.op_Inequality(activeGunTcpf, (ITxObject) null))
            flag = this.MoveLocation(locationOperation, activeGunTcpf.AbsoluteLocation);
        }
        else
          flag = this.MoveLocation(locationOperation, robot.TCPF.AbsoluteLocation);
      }
    }
    if (flag)
      return;
    int num = (int) TxMessageBox.Show(this.GetResourceManager().GetString("RLTTCPF_ERR_CANNOT_MOVE_LOCATION"), this.GetResourceManager().GetString("RLTTCPF_CMD_NAME"), MessageBoxButtons.OK, MessageBoxIcon.Hand);
  }

  public virtual string[] AdvancedDescriptions => TxCallMessages.CollectMessages((ITxCommand) this);

  private ResourceManager GetResourceManager()
  {
    return new ResourceManager("DnProcessSimulateCommands.StringTable", ((object) this).GetType().Assembly);
  }

  private ITxRoboticLocationOperation GetFirstLocation(ITxRoboticOperation operation)
  {
    ITxRoboticLocationOperation firstLocation = (ITxRoboticLocationOperation) null;
    TxObjectList txObjectList = (TxObjectList) null;
    if (operation is ITxRoboticOrderedCompoundOperation)
      txObjectList = ((ITxObjectCollection) (operation as ITxRoboticOrderedCompoundOperation)).GetAllDescendants((ITxTypeFilter) new TxTypeFilter(typeof (ITxRoboticLocationOperation)));
    else if (operation is TxRoboticSeamOperation)
      txObjectList = (operation as TxRoboticSeamOperation).GetAllDescendants((ITxTypeFilter) new TxTypeFilter(typeof (TxRoboticSeamLocationOperation)));
    if (txObjectList != null && ((Collection<ITxObject>) txObjectList).Count > 0)
      firstLocation = ((Collection<ITxObject>) txObjectList)[0] as ITxRoboticLocationOperation;
    return firstLocation;
  }

  private TxFrame GetActiveGunTCPF(TxRobot robot, ITxRoboticLocationOperation location)
  {
    ITxRoboticControllerServices controllerServices = this.GetControllerServices(robot);
    if (controllerServices == null)
      return (TxFrame) null;
    return !(controllerServices.GetActiveGun((ITxLocationOperation) location) is ITxTool activeGun) ? (TxFrame) null : activeGun.TCPF;
  }

  private ITxRoboticControllerServices GetControllerServices(TxRobot robot)
  {
    if (!(this._olpControllerUtilities.GetInterfaceImplementationFromController(robot.Controller.Name, typeof (ITxRoboticControllerServices), typeof (TxControllerAttribute), "ControllerName") is ITxRoboticControllerServices implementationFromController))
      implementationFromController = this._olpControllerUtilities.GetInterfaceImplementationFromController("default", typeof (ITxRoboticControllerServices), typeof (TxControllerAttribute), "ControllerName") as ITxRoboticControllerServices;
    if (implementationFromController == null)
      return (ITxRoboticControllerServices) null;
    implementationFromController.Init(robot);
    return implementationFromController;
  }

  private bool MoveLocation(ITxRoboticLocationOperation locationToMove, TxTransformation position)
  {
    if (locationToMove == null || TxTransformation.op_Equality(position, (TxTransformation) null) || !CApRJRobotJogUtilities.IsMovableLocation(locationToMove) || !(locationToMove is ITxLocatableObject itxLocatableObject))
      return false;
    TxTransformation absoluteLocation = itxLocatableObject.AbsoluteLocation;
    TxApplication.ActiveUndoManager.StartTransaction();
    try
    {
      itxLocatableObject.AbsoluteLocation = position;
      bool flag = CApRJRobotJogUtilities.IsLocationMovementValid(locationToMove);
      if (flag)
        TxApplication.RefreshDisplay();
      else
        itxLocatableObject.AbsoluteLocation = absoluteLocation;
      return flag;
    }
    finally
    {
      TxApplication.ActiveUndoManager.EndTransaction();
    }
  }
}
